package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Admin;
import cn.zcib.film.bean.vo.AdminVO;
import cn.zcib.film.service.AdminService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("/findById")
    public Admin doGetAdminById(HttpSession session){
        Admin admin = (Admin)session.getAttribute("loginAdmin");
        return adminService.getAdminById(admin.getaId());
    }

    @RequestMapping("/list")
    public DataGridViewResult adminList(AdminVO adminVO){
        //设置分页信息
        PageHelper.startPage(adminVO.getPage(),adminVO.getLimit());
        //调用查询角色方法
        List<Admin> ads = adminService.findAdmins(adminVO);
        //创建分页对象
        PageInfo<Admin> pageInfo = new PageInfo<>(ads);
//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/assignRole")
    public String assignRole(Integer aId,Integer rId){
        HashMap<String, Object> map = new HashMap<>();

        if (adminService.saveAdminRole(aId,rId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "分配成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "分配失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/modPwd")
    public String modPwd(Integer aId,Integer newPassword,Integer oldPassword){
        HashMap<String, Object> map = new HashMap<>();
        if (adminService.modPwd(aId,newPassword,oldPassword)){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败，原密码可能有误");
        }
        return JSON.toJSONString(map);
    }
}
